package com.example.smartmedicalsystem;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.time.DayOfWeek;

public class DayOfWeekSerializer {

    // 自定义反序列化器（将 "MON"/"TUE"/... 转为 DayOfWeek）
    public static class DayOfWeekDeserializer extends JsonDeserializer<DayOfWeek> {
        @Override
        public DayOfWeek deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
            String value = p.getText().toUpperCase();
            switch (value) {
                case "MON": return DayOfWeek.MONDAY;
                case "TUE": return DayOfWeek.TUESDAY;
                case "WED": return DayOfWeek.WEDNESDAY;
                case "THU": return DayOfWeek.THURSDAY;
                case "FRI": return DayOfWeek.FRIDAY;
                case "SAT": return DayOfWeek.SATURDAY;
                case "SUN": return DayOfWeek.SUNDAY;
                default: return DayOfWeek.valueOf(value); // 回退到标准解析
            }
        }
    }

    // 自定义序列化器（将 DayOfWeek 转为 "MON"/"TUE"/...）
    public static class DayOfWeekSerializerImpl extends JsonSerializer<DayOfWeek> {
        @Override
        public void serialize(DayOfWeek value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            switch (value) {
                case MONDAY:    gen.writeString("MON"); break;
                case TUESDAY:   gen.writeString("TUE"); break;
                case WEDNESDAY: gen.writeString("WED"); break;
                case THURSDAY:  gen.writeString("THU"); break;
                case FRIDAY:    gen.writeString("FRI"); break;
                case SATURDAY:  gen.writeString("SAT"); break;
                case SUNDAY:    gen.writeString("SUN"); break;
                default:        gen.writeString(value.name());
            }
        }
    }
}